package com.example.scan2;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.example.KeHuDuan.MediaService;
import com.example.KeHuDuan.R;


import com.example.wifi.WifiActivity;
import com.example.wifi.WifiConnector;
import com.example.wifi.WifiUtil;

import org.apache.commons.codec.binary.Base64;

public class WifiConnect extends WifiActivity {
    private static final String TAG = "WifiConnect";
    TextView state;
    String ssid = "";
    String pwd = "";

    protected MediaService.MediaBinder mServiceBinder;


    WifiConnector connector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_connect);
        state = findViewById(R.id.state);
        state.postDelayed(new Runnable() {
            @Override
            public void run() {
                wifiConnect();
            }
        }, 500);
    }



    public void wifiConnect() {
        state.setText("连接wifi...");
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        Log.d("wifiConnect", "onCreate: " + data);
//        ssid = "iPhonexsmax";
//        pwd = "12345678";
        //解析扫码结果
        StringBuffer url = new StringBuffer(data);
        Log.d("url", "onCreate:扫描 " + url);
        state.setText("扫描成功！");
        int flag = Integer.parseInt(url.substring(0, 1));
        if (flag == 1) {
            int length = Integer.parseInt(url.substring(1, 3));
            ssid = url.substring(3, 3 + length);
            pwd = url.substring(3 + length);
//            pwd = "12345678";
            pwd = decode(pwd.getBytes());
        }

        //连接wifi
        WifiUtil wifiUtil = new WifiUtil(this);

        state.setText("打开wifi...");
        boolean isOpen = wifiUtil.openWifi();
        while (!isOpen) {
            isOpen = wifiUtil.openWifi();
        }

        state.setText("开始连接wifi...");
        doConnect(ssid, pwd);
        Log.d("ssid" + ssid, "pwd: " + pwd);
    }

    @Override
    protected void sendHandlerToService(MediaService.MediaBinder iBinder) {
        mServiceBinder = iBinder;
    }

    public void init() {
        try {
            mServiceBinder.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //base64 解码
    public static String decode(byte[] bytes) {
        return new String(Base64.decodeBase64(bytes));
    }


    @Override
    protected void onConnectWifiSuccess(String msg) {
        state.setText(msg);
        Log.d(TAG, "onConnectWifiSuccess: 准备初始化Service...");
        init();
        state.postDelayed(new Runnable() {
            @Override
            public void run() {
                WifiConnect.this.finish();
            }
        }, 2500);
    }

    @Override
    public void onConnectWifiFail(String failMsg) {
        Toast.makeText(this, failMsg, Toast.LENGTH_SHORT).show();
    }
}
